package com.joker.demo.admin.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joker.demo.admin.domain.po.JobGroupPO;
import com.joker.demo.admin.domain.po.JobInfoPO;
import com.joker.demo.admin.mapper.JobGroupMapper;
import com.joker.demo.admin.router.ExecutorRouter;
import com.joker.demo.admin.router.ExecutorRouterFactory;
import com.joker.demo.admin.service.JobGroupService;
import com.joker.demo.admin.util.ListGetUtils;
import com.joker.demo.admin.util.MapGetUtils;
import com.joker.demo.core.domain.enums.JobRoutePolicyEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 注册中心服务实现类
 *
 * @author : feixiang.li
 * @since : 2025-07-27 15:11
 */
@Service
@Slf4j
public class JobGroupServiceImpl implements JobGroupService {

    @Resource
    private JobGroupMapper jobGroupMapper;

    @Override
    public Map<String, JobGroupPO> getJobGroupMap(List<String> jobGroupNames) {
        if (CollUtil.isEmpty(jobGroupNames)) {
            return MapUtil.newHashMap();
        }
        LambdaQueryWrapper<JobGroupPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(JobGroupPO::getGroupName, jobGroupNames);
        List<JobGroupPO> jobGroupList = jobGroupMapper.selectList(queryWrapper);
        return MapGetUtils.toMapWithFirstOccurrence(jobGroupList, JobGroupPO::getGroupName);
    }

    @Override
    public List<String> getAddressList(String groupJobName) {
        LambdaQueryWrapper<JobGroupPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JobGroupPO::getGroupJobName, groupJobName);
        JobGroupPO jobGroup = jobGroupMapper.selectOne(queryWrapper);
        if (Objects.isNull(jobGroup)) {
            return new ArrayList<>();
        }
        return jobGroup.getClientIdList();
    }

    @Override
    public String getAddressList(JobInfoPO jobInfo) {
        ExecutorRouter executorRouter = ExecutorRouterFactory.getExecutorRouter(JobRoutePolicyEnum.getByCode(jobInfo.getRouteStrategy()));
        List<String> addressList = getAddressList(jobInfo.getGroupJobName());
        return executorRouter.route(jobInfo.getId().toString(), addressList);
    }

    /**
     * 获取地址列表
     *
     * @param routeStrategy 路由策略
     * @param addressList   需要获取的
     * @param jobId         jobId
     * @return 运行的地址
     */
    @Override
    public String getAddressList(Integer routeStrategy, String addressList, Long jobId) {
        ExecutorRouter executorRouter = ExecutorRouterFactory.getExecutorRouter(JobRoutePolicyEnum.getByCode(routeStrategy));
        return executorRouter.route(jobId.toString(), Arrays.asList(addressList.split(",")));

    }

    /**
     * 获取地址列表
     *
     * @param jobInfoList 需要获取的地址的jobId
     * @return Map<Long, String> jobId, address
     */
    @Override
    public Map<Long, String> getAddressList(List<JobInfoPO> jobInfoList) {
        List<String> jobGroupNames = ListGetUtils.extractNonNullField(jobInfoList, JobInfoPO::getGroupJobName);
        LambdaQueryWrapper<JobGroupPO> jobInfoWrapper = new LambdaQueryWrapper<>();
        jobInfoWrapper.in(JobGroupPO::getGroupJobName, jobGroupNames);
        List<JobGroupPO> jobGroupList = jobGroupMapper.selectList(jobInfoWrapper);
        Map<String, JobGroupPO> jobGroupMap = MapGetUtils.toMapWithFirstOccurrence(jobGroupList, JobGroupPO::getGroupJobName);
        Map<Long, String> jobIdAddressMap = new HashMap<>();
        for (JobInfoPO info : jobInfoList) {
            JobGroupPO jobGroup = jobGroupMap.get(info.getGroupJobName());
            if (Objects.isNull(jobGroup)) {
                continue;
            }
            jobIdAddressMap.put(info.getId(), jobGroup.getAddressList());
        }
        return jobIdAddressMap;
    }

}
